﻿using System;
using System.Configuration;
using System.Reflection;
using System.Windows.Forms;
using mki.Backup.Helper;

namespace mki.Backup.Objects
{
	public class BackupConfiguration : ConfigurationSection
	{
		#region Fields (1)

		private static readonly BackupConfiguration settings;

		#endregion Fields

		#region Constructors (1)

		/// <summary>
		/// Initializes the <see cref="BackupConfiguration"/> class.
		/// </summary>
		static BackupConfiguration()
		{
			try
			{
				settings = ConfigurationManager.GetSection("backupConfiguration") as BackupConfiguration;
			}
			catch (Exception ex)
			{
				var message = String.Format("Error in BackupConfiguration:\n{0}", ex.Message);
				if (Assembly.GetEntryAssembly().FullName.Contains("GUI"))
				{
					MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				else { Logging.WriteError(message); }
			}
		}

		#endregion Constructors

		#region Properties (2)

		/// <summary>
		/// Gets the connection strings collection.
		/// </summary>
		/// <value>The connection strings collection.</value>
		[ConfigurationProperty("backupJobs", IsDefaultCollection = false)]
		[ConfigurationCollection(typeof(BackupJobSection), AddItemName = "backupJob", ClearItemsName = "clear", RemoveItemName = "remove")]
		public BackupJobSection BackupJobsCollection
		{
			get
			{
				return base["backupJobs"] as BackupJobSection;
			}
		}

		/// <summary>
		/// Gets the settings.
		/// </summary>
		/// <value>The settings.</value>
		public static BackupConfiguration Settings { get { return settings; } }

		#endregion Properties
	}
}